package leo.game.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.system.System;
	import flash.utils.getTimer;
	
	import leo.data.GameConfig;
	import leo.data.GameConst;
	import leo.data.GameModel;
	import leo.game.stage.Game;
	import leo.game.ui.UIBottom;
	import leo.game.ui.UIMap;
	import leo.game.ui.UITop;
	import leo.manager.BitmapManager;
	import leo.manager.McMannager;
	import leo.manager.MonsterManager;
	import leo.manager.SoundManager;
	import leo.manager.WinOrLoseManager;
	

	/**
	 * 子弹 
	 * @author leo
	 * 
	 */	
	public class Bullet extends Bitmap
	{
		/**
		 * 被攻击的目标 
		 */		
		public var target :BaseMonster;
		/**
		 * 发动攻击的目标 
		 */		
		public var attTarget :BaseTower;
		/**
		 * 是否存活 
		 */		
		public var isAlive :Boolean;
		/**
		 * 子弹名字 
		 */		
		private var bulletName :String;
		/**
		 * 
		 * @param name
		 * @param attTarget
		 * @param target
		 * @param x
		 * @param y
		 * 
		 */
		public function Bullet(name :String,attTarget :BaseTower,target :BaseMonster,x :Number,y :Number)
		{
			super();
			this.attTarget = attTarget;
			this.target = target;
			this.x = x;
			this.y = y;
			this.bulletName = name;
			this.isAlive = true;
			if(name)
			{
				var bmd :BitmapData = BitmapManager.instance.getBitmapdataByName(name);
				this.bitmapData = bmd;
			}
		}
		
		public function update():void
		{
			//子弹跟踪
			var dx :Number = target.bulletEndPoint.x  - this.x;
			var dy :Number = target.bulletEndPoint.y - this.y;
			var angle :Number = Math.atan2(dy,dx);
			var dirX :Number = (Math.cos(angle));
			var dirY :Number = (Math.sin(angle));
			var vx :Number = this.attTarget.towerVO.bulletSpeed * dirX;
			var vy :Number = this.attTarget.towerVO.bulletSpeed * dirY;
			
			this.x += vx;
			this.y += vy;
			//子弹消亡
			var monsterPoint :Point = new Point(target.bulletEndPoint.x,target.bulletEndPoint.y);
			var bulletPoint :Point = new Point(this.x,this.y);
			if(Point.distance(bulletPoint,monsterPoint) < 8)
			{
				if(target.isAlive && target.visible)
				{
					target.curHP -= attTarget.towerVO.attack;
					target.dropHP(attTarget.towerVO.attack);
					//减速子弹
					if(this.bulletName == "negativeEffectsBullet")
					{
						if(Math.random() < GameConfig.drop_speed_odds)
						{
							target.changeSpeed();
							trace("dropSpeed");
						}
					}
					//怪物死亡
					if(target.curHP <= 0)
					{
						target.isAlive = false;
						target.visible = false;
						
						GameModel.instance.curMapMonsterNum--;
						trace("金钱加50");
						UITop.instance.updateGold(target.monsterVO.obtainGold,true);
						
						if(GameModel.instance.curMapMonsterNum <= 0)
						{
							trace("小关卡胜利");
							GameModel.instance.battleNumber++;
							UIBottom.instance.resetBTN();
							MonsterManager.instance.resetMonsterManager();
							SoundManager.instance.play("WinMusic");
//							WinOrLoseManager.instance.doClearance();
						}
						
						if(GameModel.instance.battleNumber == GameConfig.monster_list.length)
						{
							trace("关卡胜利");
							WinOrLoseManager.instance.win();
							SoundManager.instance.play("PassMapMusic");
						}
					}
				}
				this.isAlive = false;
			}
		}
	}
}